//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/statline.h>
#include "logoptions.h"

enum cIDS
{
	cIDBrowseButton=5777,
	cIDSaveLogSel,
	cComboID
};


BEGIN_EVENT_TABLE(CLogOptions, wxPanel)
	EVT_BUTTON(cIDBrowseButton, CLogOptions::OnSelectLogFileName)
	EVT_CHECKBOX(cIDSaveLogSel, CLogOptions::OnTogleLogSaving) 
END_EVENT_TABLE()

CLogOptions::CLogOptions(wxWindow* pParent):wxPanel( pParent, wxID_ANY )
{
	int nFlags      = wxALL|wxALIGN_CENTRE_VERTICAL;
	int nFlagsSave  = wxRIGHT|wxALIGN_CENTRE_VERTICAL;
	int nBorder		= 5;

	wxBoxSizer* pSizer = new wxBoxSizer(wxVERTICAL);

	//Save log file options
	m_pLogFileSizer = new wxBoxSizer(wxHORIZONTAL);
	{
		m_pcbSaveLog = new wxCheckBox(this, cIDSaveLogSel, wxT(""));
		m_pLogFileSizer->Add(m_pcbSaveLog, 0, nFlagsSave, nBorder );

		wxStaticText* pTxt = new wxStaticText(this, wxID_ANY, _("Save to:"));
		m_pLogFileSizer->Add(pTxt, 0, nFlagsSave, nBorder );

		m_ptcDirName = new wxTextCtrl(this, wxID_ANY,wxT(""), wxDefaultPosition, wxSize(100, wxDefaultSize.GetY()));
		m_pLogFileSizer->Add(m_ptcDirName, 0, nFlagsSave, nBorder);

		wxButton* pBtn = new wxButton(this, cIDBrowseButton, _T("..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT|wxNO_BORDER );
		m_pLogFileSizer->Add(pBtn, 0, nFlagsSave, nBorder);

		pTxt = new wxStaticText(this, wxID_ANY, _("Size Limit(Mbyte):"));
		m_pLogFileSizer->Add(pTxt, 0, nFlagsSave, nBorder );

		wxArrayString choices;
		choices.Add(wxT("Unlimited"));
		choices.Add(wxT("1"));
		choices.Add(wxT("10"));
		choices.Add(wxT("50"));
		choices.Add(wxT("100"));
		choices.Add(wxT("200"));
		choices.Add(wxT("300"));
		choices.Add(wxT("400"));
		choices.Add(wxT("500"));
		choices.Add(wxT("600"));
		choices.Add(wxT("700"));
		choices.Add(wxT("800"));
		choices.Add(wxT("900"));
		choices.Add(wxT("1024"));
		
		m_pLimitSelector = new wxComboBox(this, wxID_ANY, wxT("Unlimited"), wxDefaultPosition, wxSize(100, wxDefaultSize.GetY()), choices, wxCB_DROPDOWN|wxCB_READONLY );
		m_pLogFileSizer->Add(m_pLimitSelector, 0, nFlagsSave, nBorder );
	}
	pSizer->Add(m_pLogFileSizer,  0, wxLEFT|wxUP|wxRIGHT,10);

	//Line
	pSizer->Add(new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxSize(10,2), wxLI_HORIZONTAL), 0, wxALL|wxEXPAND, 10);

	//Status log
	wxStaticBoxSizer* pStatusLogSizer = new wxStaticBoxSizer( new	wxStaticBox(this, wxID_ANY,
		_("Status Log")), wxVERTICAL);

	//Combo
	wxBoxSizer* pStatusTimeSizer = new wxBoxSizer(wxHORIZONTAL);

	wxStaticText* pTxt = new wxStaticText(this, wxID_ANY, _("Log status every:"));
	pStatusTimeSizer->Add(pTxt, 0, nFlagsSave, nBorder );

	m_pcLogTime  = new wxComboBox(this, cComboID, wxT(""), wxDefaultPosition, wxSize(150, wxDefaultSize.GetY()), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY  );
	pStatusTimeSizer->Add(m_pcLogTime, 0, nFlagsSave, nBorder );

	m_pcLogTime->Append( _("Disabled"),		(void*)STATUS_LOG_DISABLED);
	m_pcLogTime->Append( _("10 minutes"),	(void*)(10*60));
	m_pcLogTime->Append( _("30 minutes"),	(void*)(30*60));
	m_pcLogTime->Append( _("Hour"),			(void*)3600);

	m_pcLogTime->SetSelection(0);
	pStatusLogSizer->Add(pStatusTimeSizer,0, wxALL, nBorder);

	wxFlexGridSizer* pLogTabSizer = new wxFlexGridSizer(3, 3, 0, 0);

	m_pcbPerfomanceLog = new wxCheckBox(this, wxID_ANY, _("Perfomance"));
	pLogTabSizer->Add( m_pcbPerfomanceLog, 0, nFlags, nBorder);

	m_pcbConnectionsLog = new wxCheckBox(this, wxID_ANY, _("Connections"));
	pLogTabSizer->Add( m_pcbConnectionsLog, 0, nFlags, nBorder);

	m_pcbBandwidthLog = new wxCheckBox(this, wxID_ANY, _("Bandwidth"));
	pLogTabSizer->Add( m_pcbBandwidthLog, 0, nFlags, nBorder);

	m_pcbAccessLog = new wxCheckBox(this, wxID_ANY, _("Access"));
	pLogTabSizer->Add( m_pcbAccessLog, 0, nFlags, nBorder);

	m_pcbADSLLog = new wxCheckBox(this, wxID_ANY, _("ADSL"));
	pLogTabSizer->Add( m_pcbADSLLog, 0, nFlags, nBorder);

	m_pcbWirelessLog = new wxCheckBox(this, wxID_ANY, _("Wireless"));
	pLogTabSizer->Add( m_pcbWirelessLog, 0, nFlags, nBorder);

	pStatusLogSizer->Add(pLogTabSizer);
	
	pSizer->Add(pStatusLogSizer);

	Enable(false);

	SetSizer(pSizer);

}

CLogOptions::~CLogOptions(void)
{
}

void CLogOptions::SetEnableAppLogSave( bool bSet )
{
	m_pcbSaveLog->SetValue(bSet);
	wxCommandEvent event;
	OnTogleLogSaving(event);
}

bool CLogOptions::GetEnableAppLogSave()
{
	return m_pcbSaveLog->GetValue();
}

void CLogOptions::SetAppLogSaveFileName( const wxString& str )
{
	m_ptcDirName->SetValue(str);
}

wxString CLogOptions::GetAppLogSaveFileName()
{
	return m_ptcDirName->GetValue();
}

void CLogOptions::SetAppLogSaveLimit( long lLimit )
{
	wxString strVal;

	for ( size_t i=0;i<m_pLimitSelector->GetCount();i++ )
	{
		strVal = m_pLimitSelector->GetString( i );
		
		if ( lLimit == atol ( strVal.ToAscii() ) )
		{
			m_pLimitSelector->SetSelection( i );
			break;
		}
	}
}

long CLogOptions::GetAppLogSaveLimit()
{
	wxString strVal = m_pLimitSelector->GetValue();

	return atol ( strVal.ToAscii() );
}

////////////
//Status log
void CLogOptions::SetAppLogStatusInterval( long lLimit )
{
	void* pData;
	
	for ( size_t i=0;i<m_pcLogTime->GetCount();i++ )
	{
		pData = m_pcLogTime->GetClientData( i );
		
		if ( lLimit == (long)pData )
		{
			m_pcLogTime->SetSelection( i );
			break;
		}
	}
}

long CLogOptions::GetAppLogStatusInterval()
{
	void* pData = m_pcLogTime->GetClientData( m_pcLogTime->GetCurrentSelection());

	return (long)pData;
}

//Perfomance
void CLogOptions::SetEnablePerfomanceLog( bool bSet )
{
	m_pcbPerfomanceLog->SetValue(bSet);
}

bool CLogOptions::GetEnablePerfomanceLog()
{
	return m_pcbPerfomanceLog->GetValue();
}

//Connections
void CLogOptions::SetEnableConnectionsLog( bool bSet )
{
	m_pcbConnectionsLog->SetValue(bSet);
}

bool CLogOptions::GetEnableConnectionsLog()
{
	return m_pcbConnectionsLog->GetValue();
}

//Bandwidth
void CLogOptions::SetEnableBandwidthLog( bool bSet )
{
	m_pcbBandwidthLog->SetValue(bSet);
}

bool CLogOptions::GetEnableBandwidthLog()
{
	return m_pcbBandwidthLog->GetValue();
}

//Access
void CLogOptions::SetEnableAccessLog( bool bSet )
{
	m_pcbAccessLog->SetValue(bSet);
}

bool CLogOptions::GetEnableAccessLog()
{
	return m_pcbAccessLog->GetValue();
}

//ADSL
void CLogOptions::SetEnableADSLLog( bool bSet )
{
	m_pcbADSLLog->SetValue(bSet);
}

bool CLogOptions::GetEnableADSLLog()
{
	return m_pcbADSLLog->GetValue();
}

//Wireless
void CLogOptions::SetEnableWirelessLog( bool bSet )
{
	m_pcbWirelessLog->SetValue(bSet);
}

bool CLogOptions::GetEnableWirelessLog()
{
	return m_pcbWirelessLog->GetValue();
}



//////////////////////////////////////////////////////////////////////////////////
//Events
void CLogOptions::OnSelectLogFileName(wxCommandEvent& event)
{
	try
	{
		 wxDirDialog dlg(this, _("Choose directory to save log files"),
				wxT(""), wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );

		if ( wxID_OK ==dlg.ShowModal() )
		{
			m_ptcDirName->SetValue(dlg.GetPath());
		}
	}
	catch(...)
	{
		wxMessageBox(_("Failed!"), _("Error"), wxICON_WARNING | wxOK, this);
	}
}

void CLogOptions::OnTogleLogSaving(wxCommandEvent& event)
{
	wxSizerItemList list = m_pLogFileSizer->GetChildren();

	wxSizerItemList::iterator iter;
    for (iter = list.begin(); iter != list.end(); ++iter)
    {
       wxSizerItem *pItem = *iter;

	   if ( pItem && pItem->GetWindow()!=m_pcbSaveLog )
	   {
		   pItem->GetWindow()->Enable(m_pcbSaveLog->GetValue());
	   }
	}
}
